package com.blindnetworking;

import java.util.Iterator;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;


public class TestDB {

	
	private static DatastoreService service = DatastoreServiceFactory.getDatastoreService();
	
	public static void createPersons() {
		for(int i = 0; i < 5 ; i++) {
			Entity e = new Entity("Person");
			e.setProperty("name", "Piscozinho" + i);
			service.put(e);
		}
	}
	
	public static String getPersonName() {
		Query q = new Query("Person");
		PreparedQuery pq = service.prepare(q);
		Iterator<Entity> it = pq.asIterator();
		String result = "<h1>Lista dos Piscos</h1>";
		while(it.hasNext())
			result += "<div>" + it.next().getProperty("name")+ "</div>";
		
		return result;
	}
	
	public static void clearPersons() {
		Query q = new Query("Person");
		PreparedQuery pq = service.prepare(q);
		Iterator<Entity> it = pq.asIterator();
		while(it.hasNext())
			service.delete(it.next().getKey());
	}
}
